package TraitementMessages;

import java.util.*;
import java.util.logging.*;
import com.lo23.data.objgroup.*;
import com.lo23.data.objinterface.*;

import mainProcess.MainProcessing;
import mainProcess.UserAction;



public class GetUserInfosMessage extends InfosMessage {
	
	static final long serialVersionUID = 23201012L;
	
	public GetUserInfosMessage(String senderUID)
	{
		super(senderUID);
	}
	
	public void handleReceiption(MainProcessing mainProcessing) 
	{
		
    	LocalUser lUser = mainProcessing.getDataInterface().getLocalUser(null);

    	if(lUser == null)
    	{
    		Logger.getLogger(UserAction.class.getName()).log(Level.SEVERE, "Traitement : Local user Null");
    		return ;
    	}
    	
    	List<Friend> friendList = lUser.getListFriends();
    	
    	
    	Boolean isFriend = false;
    	
    	for (Friend anItem : friendList) 
        {
        	if (anItem.getuID().equals(senderUID)) 
        	{
        		// c'est un ami
        		isFriend = true;
        		break;
        	}
        }
		

		// si on est amis avec l'emetteur du message , on envoi les infos
		// sinon on envoi un message vide
		
    	UserProfile profileToSend;
    	
		if (isFriend)
		{
			profileToSend = lUser.getUserProfile();
		}else{
			
			profileToSend = new UserProfile();
			profileToSend.uID = lUser.getUid();
			profileToSend.login = lUser.getLogin();
			profileToSend.firstName = "";
			profileToSend.lastName = "";
			profileToSend.birthDate = new Date(0);
			profileToSend.address = "";
			profileToSend.email = "";
			profileToSend.telNumber = "";
			profileToSend.hobbies = "";
			profileToSend.port= 0;
		}
		
		UserInfosMessage reponseMessage = new UserInfosMessage(lUser.getUid(),profileToSend);
		
		List<String> listUID = new ArrayList<String>();
		listUID.add(senderUID);
		
		try 
		{
			mainProcessing.getNetworkManager().sendMessage(listUID,reponseMessage);
        } catch (Exception ex) {
            Logger.getLogger(GetUserInfosMessage.class.getName()).log(Level.SEVERE, null, ex);
            return;
        }
		
		
	}
	
}